package com.alex.DAO;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.JDBCException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.alex.entity.Tip;
import com.alex.entity.User;

public class TipDAO extends BaseDAO implements ITipDAO {
	private static final Log log = LogFactory.getLog(TipDAO.class);
//	HibernateUtil util = new HibernateUtil();

	/**
	 * 保存帖子
	 * 
	 * @param transientInstance
	 */
	public void save(Tip transientInstance) {
		Session session = getSession();
		log.debug("saving Tip instance");
		Transaction tr = null;
		tr = session.beginTransaction();
		try {
			session.save(transientInstance);
			log.debug("save successful");
			tr.commit();
			session.close();
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	/**
	 * 通过id寻找帖子
	 * 
	 * @param id
	 * @return
	 */
	public Tip findById(java.lang.Integer id) {
		Session session = getSession();
		log.debug("getting Tip instance with id: " + id);
		Transaction tr = null;
		tr = session.beginTransaction();
		try {
			Tip instance = (Tip) session.get(Tip.class, id);
			tr.commit();
			session.clear();
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/**
	 * 通过boardid寻找tiplist
	 * 
	 * @param boardId
	 * @return
	 * @throws Exception
	 */
	public List findListTip(int boardId) throws Exception {
		Session session = getSession();
		Transaction tr = null;
		List list = null;
		try {
			tr = session.beginTransaction();
			String hql = "from Tip t where t.board.boardid=" + boardId;
			Query query = session.createQuery(hql);
			list = query.list();
			tr.commit();
			session.close();
		} catch (JDBCException ex) {
			if (tr != null)
				tr.rollback();
			throw ex.getSQLException();
		}
		return list;
	}

	/**
	 * 寻找帖子回复数
	 * 
	 * @param boardId
	 * @return
	 * @throws Exception
	 */
	public int findCountTip(int boardId) throws Exception {
		Session session = getSession();
		Transaction tr = null;
		List list = null;
		try {
			tr = session.beginTransaction();
			String hql = "from Tip t where t.board.boardid=" + boardId;
			Query query = session.createQuery(hql);
			list = query.list();
			tr.commit();
			session.close();
		} catch (JDBCException ex) {
			if (tr != null)
				tr.rollback();
			throw ex.getSQLException();
		}
		return list.size();
	}

	/**
	 * 删除帖子
	 * 
	 * @param tipid
	 * @throws Exception
	 */
	public void delete(int tipid) {
		Session session = getSession();
		Transaction tr = null;
		tr = session.beginTransaction();
		try {
			Tip instance = (Tip) session.get(Tip.class, tipid);
			session.delete(instance);
			log.debug("delete successful");
			tr.commit();
			session.close();
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/**
	 * 寻找用户的帖子
	 * 
	 * @param userid
	 * @return
	 * @throws Exception
	 */
	public List findListTipByuserid(int userid) throws Exception {
		Session session = getSession();
		Transaction tr = null;
		List list = null;
		try {
			tr = session.beginTransaction();
			String hql = "from Tip t where t.user.userid=" + userid;
			Query query = session.createQuery(hql);
			list = query.list();
			tr.commit();
			session.close();
		} catch (JDBCException ex) {
			if (tr != null)
				tr.rollback();
			throw ex.getSQLException();
		}
		return list;
	}
}